<?php


namespace app\controller;


use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use think\facade\Cache;
use think\facade\Log;
use think\facade\Request;
use app\model\User;
use app\tools\ResponseTool;
class GetTelCode extends MyBaseController
{
    public function getTelCode () {
        $getTelstat = Request::param('stat');
        $templateId = '0000000';
        switch ($getTelstat) {
            case 'passwordForget':
//                $templateId = '0000000';
            case 'tel':
            case 'login':
//                $templateId = '0000000';
//                break;
            case 'register':
                $templateId = '0000000';
                break;
        }
        switch ($getTelstat) {
            case 'passwordForget':
                Log::info(1);
            case 'tel':
                Log::info(2);
            case 'login':
                Log::info(3);
                Log::info(Request::param('telNum'));
                Log::info($templateId);
                $telNum = Request::param('telNum');
                $userInfo = new User();
                $selInfo = $userInfo->where('tel_num', $telNum)->findOrEmpty();
//                Log::record($selInfo);
                if ($selInfo->isEmpty()) {
                    return ResponseTool::getRs('PARAM_ERROR',  [], '你的手机号尚未注册');
                } else {
                    $randomNum = rand(0, 9).rand(0, 9).rand(0, 9).rand(0, 9);
                    Cache::set($telNum, $randomNum, 30);
                    $client = new Client();
                    $response = $client->request('POST',
                        'http://intlsms.market.alicloudapi.com/comms/sms/groupmessaging',
                        [
                            RequestOptions::HEADERS => [
                                'Authorization' => 'APPCODE 5758dc9602f7432ea430e9184cc55f3e',
                                'Content-Type' => 'application/x-www-form-urlencoded'
                            ],
                            RequestOptions::FORM_PARAMS => [
                                'mobileSet' => '[86' . $telNum. ']',
                                'templateID' => $templateId,
                                'templateParamSet' => '["' .$randomNum.'","1"]'
                            ]
                        ]
                    );
//                    Log::info(Cache::get($telNum));
                    if ($response->getStatusCode() == '200' || $response->getReasonPhrase() == 'OK') {
                        return ResponseTool::getRs('OK', [ 'data' => json($response), '']);
                    } else {
                        return ResponseTool::getRs('GET_TELNUM_ERROR', [ 'data' => json($response), '']);
                    }
                }
                break;
            case 'register':
                $telNum = Request::param('telNum');
                $userInfo = new User();
                $selInfo = $userInfo->where('tel_num', $telNum)->findOrEmpty();
//                Log::record($selInfo);
                if (!$selInfo->isEmpty()) {
                    return ResponseTool::getRs('PARAM_ERROR',  [], '您的手机号已注册 请直接登陆');
                } else {
                    $randomNum = rand(0, 9).rand(0, 9).rand(0, 9).rand(0, 9);
                    Cache::set($telNum, $randomNum, 60);
                    $client = new Client();
                    $response = $client->request('POST',
                        'http://intlsms.market.alicloudapi.com/comms/sms/groupmessaging',
                        [
                            RequestOptions::HEADERS => [
                                'Authorization' => 'APPCODE 5758dc9602f7432ea430e9184cc55f3e',
                                'Content-Type' => 'application/x-www-form-urlencoded'
                            ],
                            RequestOptions::FORM_PARAMS => [
                                'mobileSet' => '[86' . $telNum. ']',
                                'templateID' => $templateId,
                                'templateParamSet' => '["' .$randomNum.'","1"]'
                            ]
                        ]
                    );
//                    Log::info(Cache::get($telNum));
                    if ($response->getStatusCode() == '200' || $response->getReasonPhrase() == 'OK') {
                        return ResponseTool::getRs('OK', [ 'data' => json($response), '']);
                    } else {
                        return ResponseTool::getRs('GET_TELNUM_ERROR', [ 'data' => json($response), '']);
                    }
                }
                break;
        }

    }
}